﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using PTCapture.Support;

namespace PTCapture.Forms
{
    public partial class MainForm
    {
        #region DelegateCallbacks

        /// <summary>
        /// Check if we can enable buttons
        /// </summary>
        /// <returns></returns>
        bool mainForm_ChangeCameraButtonStatus()
        {
            // Make sure we don't have the same items selected in the dropdowns
            if (((ComboBox)cameraRecorderControl1.Controls["cbVideoDevices"]).SelectedIndex ==
                ((ComboBox)cameraRecorderControl2.Controls["cbVideoDevices"]).SelectedIndex)
            {
                ((ComboBox)cameraRecorderControl2.Controls["cbVideoDevices"]).SelectedIndex = -1;
                ((ComboBox)cameraRecorderControl2.Controls["cbVideoDevices"]).Text = "";
            }
            if (((ComboBox)cameraRecorderControl1.Controls["cbAudioDevices"]).SelectedIndex ==
                ((ComboBox)cameraRecorderControl2.Controls["cbAudioDevices"]).SelectedIndex)
            {
                ((ComboBox)cameraRecorderControl2.Controls["cbAudioDevices"]).SelectedIndex = -1;
                ((ComboBox)cameraRecorderControl2.Controls["cbAudioDevices"]).Text = "";

                ((Button)cameraRecorderControl2.Controls["btnConnectCamera"]).Enabled = false;
            }


            // If both connected but not started....
            if (cameraRecorderControl1.Controls["btnStartCamera"].Enabled == true &&
                cameraRecorderControl2.Controls["btnStartCamera"].Enabled == true)
            {
                btnStartBoth.Enabled = true;
                btnStopBoth.Enabled = false;
            }
            // If both connected and started...
            else if (cameraRecorderControl1.Controls["btnStartCamera"].Enabled == false &&
                cameraRecorderControl2.Controls["btnStartCamera"].Enabled == false &&
                cameraRecorderControl1.Controls["btnStopCamera"].Enabled == true &&
                cameraRecorderControl2.Controls["btnStopCamera"].Enabled == true)
            {
                btnStartBoth.Enabled = false;
                btnStopBoth.Enabled = true;
            }
            else
            {
                btnStartBoth.Enabled = false;
                btnStopBoth.Enabled = false;
            }

            // Reassign playback videos in case we made a new one
            AssignVideoDropdowns();

            return true;
        }

        #endregion

        #region Cameras

        /// <summary>
        /// Start both connected cameras.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartBoth_Click(object sender, EventArgs e)
        {
            try
            {
                using (new HourGlass())
                {
                    btnStartBoth.Enabled = false;
                    btnStopBoth.Enabled = true;

                    cameraRecorderControl1.camera.StartRecording("Recording_" + VideoCapture.Support.TimeStamp() + "_c1");
                    cameraRecorderControl2.camera.StartRecording("Recording_" + VideoCapture.Support.TimeStamp() + "_c2");

                    cameraRecorderControl1.Controls["btnConnectCamera"].Enabled = false;
                    cameraRecorderControl1.Controls["btnStartCamera"].Enabled = false;
                    cameraRecorderControl1.Controls["btnStopCamera"].Enabled = false;

                    cameraRecorderControl2.Controls["btnConnectCamera"].Enabled = false;
                    cameraRecorderControl2.Controls["btnStartCamera"].Enabled = false;
                    cameraRecorderControl2.Controls["btnStopCamera"].Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Could not start cameras. \n{0}\n", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Stop both running cameras.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStopBoth_Click(object sender, EventArgs e)
        {
            try
            {
                using (new HourGlass())
                {
                    btnStartBoth.Enabled = false;
                    btnStopBoth.Enabled = false;

                    cameraRecorderControl1.camera.StopRecording();
                    cameraRecorderControl2.camera.StopRecording();
                    cameraRecorderControl1.camera.StopCapture();
                    cameraRecorderControl2.camera.StopCapture();

                    cameraRecorderControl1.Controls["btnConnectCamera"].Enabled = true;
                    cameraRecorderControl1.Controls["btnStartCamera"].Enabled = false;
                    cameraRecorderControl1.Controls["btnStopCamera"].Enabled = false;

                    cameraRecorderControl2.Controls["btnConnectCamera"].Enabled = true;
                    cameraRecorderControl2.Controls["btnStartCamera"].Enabled = false;
                    cameraRecorderControl2.Controls["btnStopCamera"].Enabled = false;

                    cameraRecorderControl1.Controls["cbVideoDevices"].Enabled = true;
                    cameraRecorderControl1.Controls["cbAudioDevices"].Enabled = true;
                    cameraRecorderControl2.Controls["cbVideoDevices"].Enabled = true;
                    cameraRecorderControl2.Controls["cbAudioDevices"].Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Could not start cameras. \n{0}\n", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Reassign playback videos in case we made a new one
            AssignVideoDropdowns();
        }

        #endregion
    }
}
